Crate zerocopy

source ·
Expand description

Utilities for safe zero-copy parsing and serialization.

This crate provides utilities which make it easy to perform zero-copy parsing and serialization by allowing zero-copy conversion to/from byte slices.

This is enabled by three core marker traits, each of which can be derived (e.g., #[derive(FromBytes)]):

  • FromBytes indicates that a type may safely be converted from an arbitrary byte sequence
  • AsBytes indicates that a type may safely be converted to a byte sequence
  • Unaligned indicates that a type’s alignment requirement is 1

Types which implement a subset of these traits can then be converted to/from byte sequences with little to no runtime overhead.

Note that these traits are ignorant of byte order. For byte order-aware types, see the byteorder module.

Features

alloc: By default, zerocopy is no_std. When the alloc feature is enabled, the alloc crate is added as a dependency, and some allocation-related functionality is added.

simd: When the simd feature is enabled, FromBytes and AsBytes impls are emitted for all stable SIMD types which exist on the target platform. Note that the layout of SIMD types is not yet stabilized, so these impls may be removed in the future if layout changes make them invalid. For more information, see the Unsafe Code Guidelines Reference page on the Layout of packed SIMD vectors.

simd-nightly: Enables the simd feature and adds support for SIMD types which are only available on nightly. Since these types are unstable, support for any type may be removed at any point in the future.

Re-exports

Modules

  • Byte order-aware numeric primitives.

Macros

  • Safely transmutes a value of one type to a value of another type of the same size.

Structs

  • A length- and alignment-checked reference to a byte slice which can safely be reinterpreted as another type.
  • A type with no alignment requirement.

Traits

  • Types which are safe to treat as an immutable byte slice.
  • A mutable or immutable reference to a byte slice.
  • A mutable reference to a byte slice.
  • Types for which any byte pattern is valid.
  • Types with no alignment requirement.

Functions

  • Extends a Vec<T> by pushing additional new items onto the end of the vector. The new items are initialized with zeroes.
  • Inserts additional new items into Vec<T> at position. The new items are initialized with zeroes.

Derive Macros